package com.xlh.dao.conclude.helper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xlh.dao.conclude.ConcludeCheckMapper;
import com.xlh.dao.conclude.ext.ConcludeCheckMapperExt;
import com.xlh.pojo.conclude.ConcludeCheck;
import com.xlh.pojo.conclude.example.ConcludeCheckExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cfz
 */
@Repository
public class ConcludeCheckMapperHelper {

    @Autowired
    private ConcludeCheckMapper concludeCheckMapper;


    @Autowired
    private ConcludeCheckMapperExt concludeCheckMapperExt;


    public List<ConcludeCheck> findByCondition(Long concludeId){

        ConcludeCheckExample concludeCheckExample = new ConcludeCheckExample();
        concludeCheckExample.createCriteria().andDeletedEqualTo(false).andConcludeIdEqualTo(concludeId);
        List<ConcludeCheck> concludeChecks = concludeCheckMapper.selectByExample(concludeCheckExample);

        return concludeChecks;
    }

    public int insert(ConcludeCheck concludeCheck){

        return concludeCheckMapper.insertSelective(concludeCheck);
    }

    /**
     * 删除课程检查项
     * @param concludeChecks
     */
    public void delete(List<ConcludeCheck> concludeChecks) {

        ConcludeCheck record = new ConcludeCheck();
        record.setDeleted(true);
        List<Long> ids = concludeChecks.stream().map(ConcludeCheck::getId).collect(Collectors.toList());
        ConcludeCheckExample concludeCheckExample = new ConcludeCheckExample();
        concludeCheckExample.createCriteria().andDeletedEqualTo(false).andIdIn(ids);
        concludeCheckMapper.updateByExampleSelective(record, concludeCheckExample);
    }

    /**
     * 更新操作
     * @param concludeChecks
     */
    public void updateBatch(List<ConcludeCheck> concludeChecks) {

        for (ConcludeCheck concludeCheck : concludeChecks) {
            ConcludeCheckExample concludeCheckExample = new ConcludeCheckExample();
            concludeCheckExample.createCriteria().andIdEqualTo(concludeCheck.getId());
            concludeCheck.setId(null);
            concludeCheckMapper.updateByExampleSelective(concludeCheck, concludeCheckExample);
        }
    }

    /**
     * 批量更新权重可以为空
     * @param concludeChecks
     */
    public void updateBatchWeightIsNull(List<ConcludeCheck> concludeChecks) {

        for (ConcludeCheck concludeCheck : concludeChecks) {
            if(ObjectUtil.isNotNull( concludeCheck.getId() )){
                concludeCheckMapperExt.updateByPrimaryKeySelective(concludeCheck);
            }
        }
    }




    /**
     * 根据ID获取check项
     * @param checkId
     * @return
     */
    public ConcludeCheck getById(Long checkId) {

        ConcludeCheckExample concludeCheckExample = new ConcludeCheckExample();
        concludeCheckExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(checkId);
        List<ConcludeCheck> concludeChecks = concludeCheckMapper.selectByExample(concludeCheckExample);

        if(CollectionUtil.isNotEmpty(concludeChecks)){
           return concludeChecks.get(0);
        }
        return null;
    }

    /**
     * 查询根据 目标ID
     * @param concludeId
     */
    public List<ConcludeCheck> findByConcludeId(Long concludeId) {

        ConcludeCheckExample concludeCheckExample = new ConcludeCheckExample();
        concludeCheckExample.createCriteria().andDeletedEqualTo(false).andConcludeIdEqualTo(concludeId);
        List<ConcludeCheck> concludeChecks = concludeCheckMapper.selectByExample(concludeCheckExample);

        if(CollectionUtil.isNotEmpty(concludeChecks)){
            return concludeChecks;
        }
        return null;
    }
}
